# Copyright (C) Nicola Bizzoca   2006/2007  <hannibal_smith@users.sourceforge.net>
# Copyright (C) Simone Economo   2006/2007  <ekoes@users.sourceforge.net>
# 
#    This file is part of Absynth-FTP.
#
#    Absynth-FTP is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    Absynth-FTP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk
import sys, os, shutil, stat
import time, datetime
from engine import Widgets
from lib import filesmime as mimetypes
from abcommon import *
import LocalView, TreeViewNew


class treeViewPath(LocalView.treeViewPath):
    
    def __init__(self):
        LocalView.treeViewPath.__init__(self)
        self.combobox = gtk.Entry()
        self.combobox.show()
        
    def updatePath(self, id, path):
        self.combobox.set_text(path)
        self.log.write("Remoto: %s" % path)
    
    def returnCombo(self):
        return self.combobox
#class treeViewEvents(treeViewEngine, LocalView.treeViewEngine):
    
    #def __init__(self):
        #LocalView.treeViewEngine.__init__(self)
        #treeViewEngine.__init__(self)

class treeView(LocalView.treeView, treeViewPath):
        
    def __init__(self, ftp):
        LocalView.treeView.__init__(self)
        treeViewPath.__init__(self)
        self.ftp = ftp
        self.ftp.connect('pwd', self.updatePath)
        self.pageWidget = gtk.VBox()
        self.remotetree = TreeViewNew.genericListView({
            "headers-visible"  : True,
            "headers-clickable": True,
            "rows-search"      : True,
            "rows-same-height" : False,
            "rows-alternate"   : True,
            "rows-reorder"     : False,
        })
        self.remotetree.addTextColumn("File Type", str, False, False)
        self.remotetree.addImageColumn("Filename", True, True)
        self.remotetree.addTextColumn("-", str, False, True)
        self.remotetree.addTextColumn("Size", str, True, False, 50)
        self.remotetree.addTextColumn("Last Edit", str, True, True)
        self.remotetree.mergeColumns(1, 2, None, None)
        
        pathLabel = gtk.Label('Percorso:')
        self.returnCombo()
        self.pageWidget.pack_start(pathLabel, False, False, 0)
        self.pageWidget.pack_start(self.combobox, False, True, 0)
        self.pageWidget.pack_start(self.remotetree.widget, True, True, 0)
        #self.scroll = gtk.ScrolledWindow()
        #self.scroll.add_with_viewport(self.vbox)
        #self.scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        #self.scroll.show()
        self.pageLabel = self.XMLtree.getWidget("HostnameBox").get_text()
        self.pageWidget.show_all()
        
        self.__mimetype = (os.path.isfile('/usr/bin/gnome-session') \
                       and mimetypes.Gnome()) \
                       or mimetypes.notGnome(24)
    
    def listFiles(self, defaultPath = "", showHidden = 0):
        return self.ftp.listFiles(self.__mimetype, showHidden)
    
    def loadRows(self, id, data):
        self.remotetree.clearRows()
        if self.getPath() != '/' or self.getPath() == '':
            self.remotetree.addRow(['directory', 'folder', '..', '0', '0' ]) 
        for file in data:
            self.remotetree.addRow([
            file['filetype'], file['mime'], file['filename'], file['size'], file['date'] ])
    
    def returnIndex(self):
        return self.index

    def pwd(self): 
        return self.getPath()
        
    #def delete(self, name): self.__FTPEngine.delete(name)
        
    #def rmdir(self, name): pass
        
class UI(treeView, LocalView.UI):
    def __init__(self, ftp, logger):
        self.log = logger
        self.ftp = ftp
        LocalView.treeView.__init__(self)
        treeView.__init__(self, ftp)
        self.connectEvents()
    
    def onRowClick(self, *args):
        model, iter = self.remotetree.widget.get_selection().get_selected()
        file = {
            "type": model.get_value(iter, 0),
            "name": model.get_value(iter, 2),
            "size" : model.get_value(iter, 3),
        }
        if file["type"] == "directory":
            self.ftp.cwd(file["name"])
        else:
            self.ftp.download(file["name"], file["size"], self.getPath())

    def connectEvents(self):
        self.remotetree.widget.connect('row_activated', self.onRowClick)
        self.XMLtree.connectSignals({
            "on_RemotePathText_activate": self.onPathChange,
        })
        self.ftp.connect('listed', self.loadRows)